cap log close
log using "${logpath}district_regs.log", replace

/*******************************************************************************
district_regs.do

This code runs district-level regressions
*******************************************************************************/

use "${cleandatapath}ambulancetaxis_firmpanel_enforcement.dta", clear

qui gen firmactive = 1 if linepmt > 0

collapse (sum) firmactive (min) district_treat_civil (min) district_treat_criminal (min) padate, by(district month)

gen postpa = (month >= padate & !mi(padate))
gen postcrim = (month >= mofd(district_treat_criminal) & !mi(padate))
gen postciv = (month >= mofd(district_treat_civil) & !mi(padate))

encode district, gen(dist_code)

*********************************************
*** Civil enforcement DD

qui {

	*** Define relative time for treated 
	gen civil_month= mofd(district_treat_civil)
	gen reltime_civ = month - civil_month

	* Drop districts treated near end or beginning of panel
	replace reltime_civ=. if civil_month >= mofd(mdy(1,1,2018)) & !mi(civil_month)
	replace reltime_civ=. if civil_month < mofd(mdy(1,1,2009)) & !mi(civil_month)

	** Window treated districts 
	replace reltime_civ = . if ( (reltime_civ >= 24) | (reltime_civ < -24)) & !mi(reltime_civ)

	*** Make dummy reltime amount for controls
	replace reltime_civ = -50 if mi(district_treat_civil)

	** Fix the negative levels issue in stata 
	gen reltime_civ2 = reltime_civ + 100

	*** Create static post-civ variable 
	gen post_together_civ = reltime_civ2
	replace post_together_civ = 100 if (reltime_civ >= 0) & (reltime_civ < 24)

	* Make log outcome
	gen lfirm = log(firmactive + 1)

}

*Dynamic
reghdfe lfirm ib99.reltime_civ2 postpa postcrim, absorb(district month) vce(cluster district) // Used in Figure A18b
coefplot, vertical baselevels keep(*reltime*) recast(connected) ciopts(recast(rcap) color(navy)) xlab(1 "-24" 7 "-18" 13 "-12" 19 "-6" 25 "0" 31 "6" 37 "12" 43 "18" 49 "24") xline(25, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) xti("Months Since First Civil Enforcement") graphregion(color(white)) bgcolor(white) ylab(, angle(0)) yti("Active Firms in District (Logs)")
qui graph export "${outpath}Figure_11a.pdf", replace

qui {
	
	*Static
	gen post_together=post_together_civ
	reghdfe firmactive ib99.post_together postpa postcrim, absorb(district month) vce(cluster district)
	estadd ysumm
	estimates save "${estpath}firmactive_civ", replace
	
}

reghdfe lfirm ib99.post_together postpa postcrim, absorb(district month) vce(cluster district) // Used in calculations for Table A32

qui {
	
	estadd ysumm
	estimates save "${estpath}lfirm_civ", replace

	estimates clear
	eststo: reg lfirm ib99.post_together_civ postpa postcrim i.dist_code i.month
	
}

*********************************************
*** Criminal enforcement DD

qui {

	*** Define relative time for treated 
	gen crim_month= mofd(district_treat_criminal)
	gen reltime_crim = month - crim_month


	* Drop districts treated near end or beginning of panel
	replace reltime_crim=. if crim_month >= mofd(mdy(1,1,2018)) & !mi(crim_month)
	replace reltime_crim=. if crim_month < mofd(mdy(1,1,2009)) & !mi(crim_month)

	** Window treated districts 
	replace reltime_crim = . if ( (reltime_crim >= 24) | (reltime_crim < -24)) & !mi(reltime_crim)
		* Keep them around so that in crim regression, post-civil isn't dropped and vice versa
		
	*** Make dummy reltime amount for controls
	replace reltime_crim = -50 if mi(district_treat_criminal)

	** Fix the negative levels issue in stata 
	gen reltime_crim2 = reltime_crim + 100

	*** Create static post-crim variable 
	gen post_together_crim = reltime_crim2
	replace post_together_crim = 100 if (reltime_crim >= 0) & (reltime_crim < 24)

}

reghdfe lfirm ib99.reltime_crim2 postpa postciv, absorb(district month) vce(cluster district) // Used in Figure A18b
coefplot, vertical baselevels keep(*reltime*) recast(connected) ciopts(recast(rcap) color(navy)) xlab(1 "-24" 7 "-18" 13 "-12" 19 "-6" 25 "0" 31 "6" 37 "12" 43 "18" 49 "24") xline(25, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) xti("Months Since First Criminal Enforcement") graphregion(color(white)) bgcolor(white) ylab(, angle(0)) yti("Active Firms in District (Logs)")
qui graph export "${outpath}Figure_11b.pdf", replace

qui {
	
	*Static
	replace post_together=post_together_crim
	reghdfe firmactive ib99.post_together postpa postciv, absorb(district month) vce(cluster district)
	estadd ysumm
	estimates save "${estpath}firmactive_crim", replace
	
}

reghdfe lfirm ib99.post_together postpa postciv, absorb(district month) vce(cluster district) // Used in calculations for Table A32

qui {
	
	estadd ysumm
	estimates save "${estpath}lfirm_crim", replace

	eststo: reg lfirm ib99.post_together_crim postpa postciv i.dist_code i.month

	suest est1 est2, vce(cluster district)

}

test [est1_mean]100.post_together_civ=[est2_mean]100.post_together_crim // Included in Table A32

qui {
	*Table 8
	estimates clear
	estimates use "${estpath}lfirm_civ"
	eststo est1
	eststo est1,addscalars(Year_FE 1)
	eststo est1,addscalars(Dist_FE 1)
	estimates use "${estpath}firmactive_civ"
	eststo est2
	eststo est2,addscalars(Year_FE 1)
	eststo est2,addscalars(Dist_FE 1)
	estimates use "${estpath}lfirm_crim"
	eststo est3
	eststo est3,addscalars(Year_FE 1)
	eststo est3,addscalars(Dist_FE 1)
	estimates use "${estpath}firmactive_crim"
	eststo est4
	eststo est4,addscalars(Year_FE 1)
	eststo est4,addscalars(Dist_FE 1)
	
	label var lfirm "\shortstack{Active \\ Firms (Log)}"
	label var firmactive "\shortstack{Active \\ Firms}"
	label define post 100  "Enforcement"
	label values post_together post
	
	esttab using "${outpath}Table_8.tex", ///
		replace label se frag booktabs keep(*100*) ///
		star(+ 0.1 * 0.05 ** 0.01 *** 0.001) ///
		mgroups("Civil" "Criminal", pattern(1 0 1 0) ///
			prefix(\multicolumn{@span}{c}{) suffix(}) span ///
			erepeat(\cmidrule(lr){@span})) ///
		stats(Year_FE Dist_FE ymean N, ///
			label("Year-Month FE" "District FE" "Dep. Var. Mean" "Observations"))
}

di exp(-0.0442)-1 // Referenced in Section 5.4, Paragraph 1

log close